package com.example.babymonitorcase;

import java.io.IOException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	private TextView txtResultado;
	private String URL = "";
	private String NAMESPACE = "http://";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtResultado = (TextView) findViewById(R.id.txtResultado);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    public void obtenerPersonaSS(View v) {
		txtResultado.setText("");
		new ObtenerPersonaSSBackground().execute();
	}
    
    class ObtenerPersonaSSBackground extends AsyncTask<Void, Void, String> {

		/* (non-Javadoc)
		 * @see android.os.AsyncTask#doInBackground(Params[])
		 */
		@Override
		protected String doInBackground(Void... params) {

			String resultadoJSON = "";
			String METHOD_NAME = "";	//Nombre del metodo al cual debe llamar.
			String SOAP_ACTION = NAMESPACE + "/" + METHOD_NAME;
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
			SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			soapEnvelope.dotNet = true;
			soapEnvelope.implicitTypes = true;
			soapEnvelope.setOutputSoapObject(request);

			HttpTransportSE aht = new HttpTransportSE(URL);
			Log.d("Cliente SOAP", "Iniciando Consulta");
			try {
				aht.call(SOAP_ACTION, soapEnvelope);
				SoapPrimitive resultString = (SoapPrimitive) soapEnvelope
						.getResponse();
				resultadoJSON = resultString.toString();
				Log.d("Cliente SOAP", "Finalizando Consulta");

			} catch (IOException e) {
				e.printStackTrace();
			} catch (XmlPullParserException e) {
				e.printStackTrace();
			}

			return resultadoJSON;
		}

		/* (non-Javadoc)
		 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
		 */
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			try {
				String texto = "";
				JSONObject resultado = new JSONObject(result);
				texto += resultado.getString("Titulo") + "\n";
				String contenido = resultado.getString("Contenido");
				Log.d("ClienteSoap", contenido);
				JSONArray PersonaSSJSONArray = new JSONArray(contenido);
				int cantPeliculas = PersonaSSJSONArray.length();
				for (int i = 0; i < cantPeliculas; i++) {
					texto += PersonaSSJSONArray.getJSONObject(i).getString(
							"idPersonaSS")
							+ "\n";
					texto += PersonaSSJSONArray.getJSONObject(i).getString(
							"nombre")
							+ "\n";
					texto += PersonaSSJSONArray.getJSONObject(i).getString(
							"apellido1")
							+ "\n";
					texto += PersonaSSJSONArray.getJSONObject(i).getString(
							"apellido2")
							+ "\n";
					texto += PersonaSSJSONArray.getJSONObject(i).getString(
							"username")
							+ "\n";
				}
				txtResultado.setText(texto);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}
    
    
}
